package Vocabulary.View;
import Vocabulary.Mapper.WordMapper;
import Vocabulary.MybatisUtils;
import Vocabulary.Word;
import javazoom.jl.player.Player;
import org.apache.ibatis.session.SqlSession;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static Vocabulary.MybatisUtils.convertByteArrayToList;
import static Vocabulary.Test.MP3Player.convertByteToInputStream;
import static Youdao.Test.Test.textToSpeech;
public class Home extends JFrame implements MouseMotionListener,MouseListener{
    List<List<Word>>wordss;
    public JButton previous =new JButton("上一页");
    public JButton next =new JButton("下一页");
    public JPanel contentPane = new JPanel();
    JTextField jsearch =new JTextField("");
    private final JButton search=new JButton("搜索");
    JPanel jPanel;
    JPanel jPanel1;
    JPanel jPanel2;
    ImageIcon background1 = new ImageIcon("src\\image\\p.jpg");
    public List<Word> words;
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
    JPanel Detail;
    MenuBar jMenuBar;
    Menu jMenu;
    MenuItem menuItem=new MenuItem("撤退");
    public Home() {
        jMenuBar=new MenuBar();
        jMenu=new Menu("撤退");
        jMenu.add(menuItem);
        jMenuBar.add(jMenu);
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setContentPane(contentPane);
                setBounds(100, 100, 1000, 730);
            }
        });
        this.setMenuBar(jMenuBar);
        jPanel1=new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),contentPane);
            }

        };
        jPanel2=new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),contentPane);
            }
        };
        jPanel2.setSize(getSize().width/3,getSize().height/3);
        contentPane.setLayout(new BorderLayout());

        jPanel2.setLayout(new BorderLayout());
        jsearch.setColumns(15);
        jPanel2.add(jsearch,BorderLayout.CENTER);
        search.setSize(100,50);
        jPanel2.add(search,BorderLayout.EAST);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBackground(Color.white);
        setTitle("主页");
        setBounds(100, 100, 600, 450);
        JPanel panel;
        contentPane.setBorder(new EmptyBorder(50, 50, 50, 50));
        setContentPane(contentPane);
        ImageIcon background = new ImageIcon("src\\image\\p.jpg");
        setSize(background.getIconWidth(),background.getIconHeight()*6/7);
        panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(), this);
                g.setFont(new Font("宋体", Font.BOLD,60));
                g.setColor(Color.BLUE);
            }
        };
        jPanel=new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),contentPane);
            }
        };
        jPanel.setLayout(new GridLayout(15,1));
        contentPane.add(jPanel,BorderLayout.CENTER);
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        contentPane.setLayout(new BorderLayout());
        setContentPane(contentPane);
        contentPane.setOpaque(false);
        panel.setBackground(Color.white);
        contentPane.add(panel);
        this.addMouseMotionListener(this);
        this.addMouseMotionListener(this);
        JPanel jPanel1=new JPanel();
        jPanel1.setLayout(new GridLayout());
        previous.setSize(100,40);
        jPanel1.add(previous,BorderLayout.SOUTH);
        previous.setOpaque(true);
        next.setSize(100,40);
        next.setOpaque(true);
        jPanel1.add(next,BorderLayout.SOUTH);
        previous.addMouseListener(this);
        next.addMouseListener(this);
        this.setVisible(true);
        jPanel1.setLayout(new GridLayout(1,2));

        contentPane.add(jPanel1,BorderLayout.SOUTH);
        contentPane.add(jPanel2,BorderLayout.NORTH);
        this.addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension screenSize =getSize();
                jPanel.setBounds(50,40,screenSize.width*9/10,screenSize.height*4/5);
            }
            @Override
            public void componentMoved(ComponentEvent e) {

            }
            @Override
            public void componentShown(ComponentEvent e) {
            }
            @Override
            public void componentHidden(ComponentEvent e) {
            }
        });
        jsearch.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
        jsearch.getActionMap().put("enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                search.doClick();  // 模拟按钮点击操作
            }
        });
        search.addActionListener(e -> {
            String search=jsearch.getText();
            jPanel.removeAll();
            words = wordMapper.getWordsByKey(search);
            wordss=splitListIntoGroupsOf30(words);
            try {
                for (Word word:wordss.get(0)){
                    JButton jbutton=new JButton(word.getWords());
                    jbutton.setForeground(Color.RED);
                    jbutton.setSize(100,50);
                    jbutton.setOpaque(false);
                    jbutton.setContentAreaFilled(false);
                    jbutton.setBorderPainted(false);
                    jbutton.addMouseListener(new MouseListener() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            String str=jbutton.getText();
                            Word word1 = wordMapper.GetWordByKey(str);
                            Detail=new JPanel(){
                                @Override
                                protected void paintComponent(Graphics g) {
                                    super.paintComponent(g);
                                    g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),this);
                                }
                            };
                            Detail.setLayout(new GridLayout(3,1));

                            JButton w = new JButton(word.getWords());
                            w.setFont(new Font("彩云",Font.BOLD,60));
                            w.setBorderPainted(false);
                            w.setContentAreaFilled(false);
                            Detail.add(w);

                            JButton p = new JButton(word1.getExplain());
                            p.setBorderPainted(false);
                            p.setFont(new Font("彩云",Font.BOLD,60));
                            p.setContentAreaFilled(false);
                            p.addMouseListener(new MouseListener() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    textToSpeech(p.getText());
                                }
                                @Override
                                public void mousePressed(MouseEvent e) {

                                }

                                @Override
                                public void mouseReleased(MouseEvent e) {

                                }

                                @Override
                                public void mouseEntered(MouseEvent e) {

                                }

                                @Override
                                public void mouseExited(MouseEvent e) {

                                }
                            });
                            Detail.add(p);
                            w.addMouseListener(new MouseListener() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    try {
                                        Player player = new Player(convertByteToInputStream(word1.getAudio()));
                                        player.play();
                                        System.out.println("0000");
                                    } catch (Exception ex) {
                                        throw new RuntimeException(ex);
                                    }
                                }
                                @Override
                                public void mousePressed(MouseEvent e) {
                                }
                                @Override
                                public void mouseReleased(MouseEvent e) {

                                }
                                @Override
                                public void mouseEntered(MouseEvent e) {

                                }
                                @Override
                                public void mouseExited(MouseEvent e) {

                                }
                            });
                            JButton sentence=new JButton("对不起暂时缺少资源");
                            Random random = new Random();
                            try {
                                List<byte[]> bytes = convertByteArrayToList(word1.getInformation(), word1.getInformation().length);
                                List<String>convertword=new ArrayList<>();
                                for (byte[] bytes1:bytes){
                                    String s = new String(bytes1, StandardCharsets.UTF_8);
                                    String[] split = s.split("\\.");
                                    List<String>list= Arrays.stream(split).toList();
                                    for (String s1:list){
                                        String[] split1 = s1.split("\\?");
                                        List<String> list1 = Arrays.stream(split1).toList();
                                        for (String s2:list1){
                                            convertword.add(s2);
                                            System.out.println(s2);
                                        }
                                    }
                                    System.out.println(convertword.size());
                                    sentence=new JButton(convertword.get(8));
                                }
                            } catch (Exception ex) {
                                System.out.println("000000000000");
                            }
                            sentence.setBorderPainted(false);
                            sentence.setFont(new Font("彩云",Font.BOLD,20));
                            sentence.setContentAreaFilled(false);
                            Detail.add(sentence);
                            JButton finalSentence = sentence;
                            sentence.addMouseListener(new MouseListener() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    textToSpeech(finalSentence.getText());
                                }

                                @Override
                                public void mousePressed(MouseEvent e) {

                                }

                                @Override
                                public void mouseReleased(MouseEvent e) {

                                }

                                @Override
                                public void mouseEntered(MouseEvent e) {

                                }

                                @Override
                                public void mouseExited(MouseEvent e) {

                                }
                            });

                            setContentPane(Detail);
                            setBounds(100,100,800,600);
                            setVisible(true);
                        }

                        @Override
                        public void mousePressed(MouseEvent e) {

                        }

                        @Override
                        public void mouseReleased(MouseEvent e) {

                        }

                        @Override
                        public void mouseEntered(MouseEvent e) {

                        }

                        @Override
                        public void mouseExited(MouseEvent e) {

                        }
                    });
                    jPanel.add(jbutton);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(jPanel, "空的", "失败:", JOptionPane.ERROR_MESSAGE);
            }
            jPanel.repaint();
            jPanel.revalidate();
        });
        search.addMouseListener(this);

    }
    int i=0;
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

        if (e.getSource()== next){
            try {
                jPanel.removeAll();
                for (Word word:wordss.get(i)){
                    JButton jbutton=new JButton(word.getWords());
                    jbutton.setForeground(Color.RED);
                    jbutton.setSize(100,50);
                    jbutton.setOpaque(false);
                    jbutton.setContentAreaFilled(false);
                    jbutton.setBorderPainted(false);
                    jbutton.addMouseListener(new MouseListener() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            String str=jbutton.getText();
                            Word word1 = wordMapper.GetWordByKey(str);
                            System.out.println(Arrays.toString(word1.getAudio()));
                        }

                        @Override
                        public void mousePressed(MouseEvent e) {

                        }

                        @Override
                        public void mouseReleased(MouseEvent e) {

                        }

                        @Override
                        public void mouseEntered(MouseEvent e) {

                        }

                        @Override
                        public void mouseExited(MouseEvent e) {

                        }
                    });
                    jPanel.add(jbutton);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "这是最后一页", "失败:", JOptionPane.ERROR_MESSAGE);
                i=-1;
            }
            jPanel.repaint();
            jPanel.revalidate();
            System.out.println("第"+i+"页");
            i++;
        }
        if (e.getSource()== previous){

            try {
                jPanel.removeAll();
                wordss=splitListIntoGroupsOf30(words);
                for (Word word:wordss.get(i)){
                    JButton jbutton=new JButton(word.getExplain());
                    jbutton.setForeground(Color.RED);
                    jbutton.setSize(100,50);
                    jbutton.setOpaque(false);
                    jbutton.setContentAreaFilled(false);
                    jbutton.setBorderPainted(false);
                    jbutton.addMouseListener(new MouseListener() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            String str=jbutton.getText();
                            Word word1 = wordMapper.GetWordByKey(str);
                            System.out.println(word1);
                            new WordsDetailed(word1);
                        }

                        @Override
                        public void mousePressed(MouseEvent e) {
                        }
                        @Override
                        public void mouseReleased(MouseEvent e) {
                        }
                        @Override
                        public void mouseEntered(MouseEvent e) {
                        }
                        @Override
                        public void mouseExited(MouseEvent e) {
                        }
                    });
                    jPanel.add(jbutton);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "这是第一页", "失败:", JOptionPane.ERROR_MESSAGE);
            }
            jPanel.repaint();
            jPanel.revalidate();
            System.out.println("第"+i+"页");
            i--;
        }
        if (e.getSource()==search){
            jPanel.removeAll();
            for (Word word:wordss.get(i)){
                JButton jButton=new JButton(word.getWords());
                jButton.setForeground(Color.RED);
                jButton.setOpaque(true);
                jPanel.add(jButton);
                jButton.setContentAreaFilled(false);
                jButton.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        String str=jButton.getText();
                        Word word1 = wordMapper.GetWordByKey(str);

                        Detail=new JPanel(){
                            @Override
                            protected void paintComponent(Graphics g) {
                                super.paintComponent(g);
                                g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),this);
                            }
                        };
                        Detail.setLayout(new GridLayout(3,1));

                        JButton w = new JButton(word.getWords());
                        w.setFont(new Font("彩云",Font.BOLD,60));
                        w.setBorderPainted(false);
                        w.setContentAreaFilled(false);
                        Detail.add(w);

                        JButton p = new JButton(word1.getExplain());
                        p.setBorderPainted(false);
                        p.setFont(new Font("彩云",Font.BOLD,60));
                        p.setContentAreaFilled(false);
                        p.addMouseListener(new MouseListener() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                textToSpeech(p.getText());
                            }
                            @Override
                            public void mousePressed(MouseEvent e) {

                            }

                            @Override
                            public void mouseReleased(MouseEvent e) {

                            }

                            @Override
                            public void mouseEntered(MouseEvent e) {

                            }

                            @Override
                            public void mouseExited(MouseEvent e) {

                            }
                        });
                        Detail.add(p);
                        w.addMouseListener(new MouseListener() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                try {
                                    Player player = new Player(convertByteToInputStream(word1.getAudio()));
                                    player.play();
                                    System.out.println("0000");
                                } catch (Exception ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                            @Override
                            public void mousePressed(MouseEvent e) {
                            }
                            @Override
                            public void mouseReleased(MouseEvent e) {

                            }
                            @Override
                            public void mouseEntered(MouseEvent e) {

                            }
                            @Override
                            public void mouseExited(MouseEvent e) {

                            }
                        });
                        JButton sentence=new JButton("对不起暂时缺少资源");
                        Random random = new Random();try {
                            List<byte[]> bytes = convertByteArrayToList(word1.getInformation(), word1.getInformation().length);
                            List<String>convertword=new ArrayList<>();
                            for (byte[] bytes1:bytes){
                                String s = new String(bytes1, StandardCharsets.UTF_8);
                                String[] split = s.split("\\.");
                                List<String>list= Arrays.stream(split).toList();
                                for (String s1:list){
                                    String[] split1 = s1.split("\\?");
                                    List<String> list1 = Arrays.stream(split1).toList();
                                    for (String s2:list1){
                                        convertword.add(s2);
                                        System.out.println(s2);
                                    }
                                }
                                System.out.println(convertword.size());
                                sentence=new JButton(convertword.get(8));
                            }
                        } catch (Exception ex) {
                            System.out.println("000000000000");
                        }
                        sentence.setBorderPainted(false);
                        sentence.setFont(new Font("彩云",Font.BOLD,20));
                        sentence.setContentAreaFilled(false);
                        Detail.add(sentence);
                        JButton finalSentence = sentence;
                        sentence.addMouseListener(new MouseListener() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                textToSpeech(finalSentence.getText());
                            }

                            @Override
                            public void mousePressed(MouseEvent e) {

                            }

                            @Override
                            public void mouseReleased(MouseEvent e) {

                            }

                            @Override
                            public void mouseEntered(MouseEvent e) {

                            }

                            @Override
                            public void mouseExited(MouseEvent e) {

                            }
                        });
                        setContentPane(Detail);
                        setBounds(100,100,800,600);
                        setVisible(true);
                    }
                    @Override
                    public void mousePressed(MouseEvent e) {

                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                });
                jButton.setBorderPainted(false);
            }
            jPanel.repaint();
            jPanel.revalidate();
            System.out.println(Arrays.toString(jPanel.getComponents()));
        }
        if (e.getSource()==jMenu){
            setContentPane(jPanel);
        }
    }
    public static void main(String[] args) {
        new Home().setVisible(true);
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    public static List<List<Word>> splitListIntoGroupsOf30(List<Word> wordList) {
        List<List<Word>> resultList = new ArrayList<>();
        int size = wordList.size();
        for (int i = 0; i < size; i += 30) {
            int end = Math.min(size, i + 30);
            resultList.add(wordList.subList(i, end));
        }
        return resultList;
    }
}
